package com.pansoft.openplanet.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.efounder.baseadapter.listview.CommonAdapter;
import com.efounder.baseadapter.listview.ViewHolder;
import com.efounder.chat.activity.BaseActivity;
import com.efounder.chat.utils.LXGlideImageLoader;
import com.efounder.chat.widget.RoundImageView;
import com.efounder.constant.EnvironmentVariable;
import com.efounder.http.EFHttpRequest;
import com.efounder.utils.ResStringUtil;
import com.google.gson.Gson;
import com.pansoft.openplanet.R;
import com.pansoft.openplanet.bean.EscrowAccountListBean;
import com.pansoft.openplanet.util.TCMathUtil;
import com.pansoft.openplanet.util.TCRequestUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.utilcode.util.ToastUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 托管账户
 *
 * @author yqs
 */

public class EscrowAccountActivity extends BaseActivity implements View.OnClickListener {

    private ImageView ivBack;
    private TextView tvTitle;

    List<Integer> iamges;
    private ListView listview;
    private List<EscrowAccountListBean.AssetsBean> mDatas;
    private MyAdater myAdater;
    private SmartRefreshLayout refreshLayout;


    public static void start(Context context) {
        start(context, false);
    }

    public static void start(Context context, boolean isSelect) {
        Intent starter = new Intent(context, EscrowAccountActivity.class);
        starter.putExtra("isSelectMode", isSelect);
        if (isSelect) {
            ((Activity) context).startActivityForResult(starter, 100);
        } else {
            context.startActivity(starter);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.assets_activity_common_tuoguan_account);
        initView();
        //initListener();

        initData();
    }


    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EFHttpRequest.cancelRequest(TAG);
    }

    private void initView() {
        //标题
        listview = (ListView) findViewById(R.id.listview);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        ivBack = (ImageView) findViewById(R.id.iv_back);
        refreshLayout = (SmartRefreshLayout) findViewById(R.id.refreshLayout);
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                requestData();
            }
        });
    }


    private void initData() {
        tvTitle.setText(ResStringUtil.getString(R.string.open_planet_escrow_account));
        if (getIntent().getBooleanExtra("isSelectMode", false)) {
            tvTitle.setText(ResStringUtil.getString(R.string.open_planet_choose_red_packet_token));
        }
        mDatas = new ArrayList<>();
        myAdater = new MyAdater(this, R.layout.assets_item_tuoguan_account, mDatas);
        listview.setAdapter(myAdater);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (getIntent().getBooleanExtra("isSelectMode", false)) {
                    Intent intent = new Intent();
                    intent.putExtra("assetBean", mDatas.get(position));
                    setResult(Activity.RESULT_OK, intent);
                    finish();
                    return;
                }
                EscrowRecordActivity.start(EscrowAccountActivity.this, mDatas.get(position));
            }
        });
        refreshLayout.autoRefresh();

    }

    private void requestData() {
        final HashMap<String, String> map = new HashMap<>();
        map.put("userId", EnvironmentVariable.getUserName());

        TCRequestUtil.getCommonRequest(TAG, "trusteeship/assets", map, new TCRequestUtil.TCRequestCallback() {
            @Override
            public void onSuccess(String response) {
                refreshLayout.finishRefresh(100);
                mDatas.clear();
                myAdater.notifyDataSetChanged();
                try {
                    EscrowAccountListBean escrowAccountListBean = new Gson().fromJson(response, EscrowAccountListBean.class);
                    if (escrowAccountListBean == null) {
                        ToastUtils.showShort(R.string.common_text_http_request_fail);
                        return;
                    }
                    if ("success".equals(escrowAccountListBean.getResult())) {
                        mDatas.addAll(escrowAccountListBean.getAssets());
                        myAdater.notifyDataSetChanged();
                    } else {
                        ToastUtils.showShort(R.string.common_text_http_request_fail);

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    refreshLayout.finishRefresh(100);
                    ToastUtils.showShort(R.string.common_text_http_request_fail);
                }
            }

            @Override
            public void onFail(String error) {
                refreshLayout.finishRefresh(100);
                ToastUtils.showShort(R.string.common_text_http_request_fail);

            }
        });
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.but_send) {
            //点击发送
        } else if (id == R.id.ll_wallet_layout) {

        }
    }

    public class MyAdater extends CommonAdapter<EscrowAccountListBean.AssetsBean> {
        public MyAdater(Context context, int layoutId, List<EscrowAccountListBean.AssetsBean> datas) {
            super(context, layoutId, datas);
        }

        @Override
        protected void convert(ViewHolder viewHolder, EscrowAccountListBean.AssetsBean item, int position) {
            RoundImageView ivIcon;
            TextView tvTitle;
            TextView tvCanuse;
            TextView tvDisabled;

            ivIcon = (RoundImageView) viewHolder.getView(R.id.iv_icon);
            tvTitle = (TextView) viewHolder.getView(R.id.tv_title);
            tvCanuse = (TextView) viewHolder.getView(R.id.tv_canuse);
            tvDisabled = (TextView) viewHolder.getView(R.id.tv_disabled);
            LXGlideImageLoader.getInstance().displayImage(ivIcon.getContext(), ivIcon, item.getCoinIcon());
            tvTitle.setText(item.getCoinName());
            tvDisabled.setText(TCMathUtil.retainAsignDecimals(item.getFreezingCoin(), 8));
            tvCanuse.setText(TCMathUtil.retainAsignDecimals(item.getAvailableCoin(), 8));
            if (item.getFreezingCoin() == 0.0d) {
                tvDisabled.setText("0.00000000");
            }
            if (item.getAvailableCoin() == 0.0d) {
                tvCanuse.setText("0.00000000");
            }
        }
    }
}
